package com.ybg.report.row.dao;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.ybg.base.jdbc.BaseDao;
import com.ybg.base.jdbc.BaseMap;
import com.ybg.base.jdbc.util.QvoConditionUtil;
import com.ybg.base.util.Page;
import com.ybg.report.row.domain.RowVO;
import com.ybg.report.row.qvo.RowQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import com.ybg.base.jdbc.DataBaseConstant;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
/**
 * 
 * 
 * @author Deament
 * @email 591518884@qq.com
 * @date 2018-02-21
 */
@Repository
public class RowDaoImpl extends BaseDao implements RowDao {
  
	@Autowired
  /**注入你需要的数据库**/
	@Qualifier(DataBaseConstant.JD_REPORT)
	JdbcTemplate jdbcTemplate;
  
	@Override
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	private static String  QUERY_TABLE_COLUMN= "  row.rowname, row.rowkey, row.rownum, row.gmt_create, row.gmt_modified, row.reportid, row.id";
	private static String  QUERY_TABLE_NAME="report_row  row";
	@Override
	public RowVO save(RowVO row) throws Exception {
		BaseMap<String, Object> createmap = new BaseMap<String, Object>();
		String id = null;		
		 createmap.put("rowname", row.getRowname());
		 createmap.put("rowkey", row.getRowkey());
		 createmap.put("rownum", row.getRownum());
		 createmap.put("gmt_create", row.getGmtCreate());
		 createmap.put("gmt_modified", row.getGmtModified());
		 createmap.put("reportid", row.getReportid());
		 		id = baseCreate(createmap, "report_row", "id");
		row.setId((String) id);
		return row;
	}
	@Override
	public void update(BaseMap<String, Object> updatemap,
			BaseMap<String, Object> WHEREmap) {
		this.baseupdate(updatemap, WHEREmap, "report_row");
	}
	@Override
	public Page list(Page page,  RowQuery qvo)throws Exception {
		StringBuilder sql = new StringBuilder();
		sql.append(SELECT).append(QUERY_TABLE_COLUMN).append(FROM)
				.append(QUERY_TABLE_NAME);
		sql.append(getcondition(qvo));
		page.setTotals(queryForInt(sql));
		if (page.getTotals() > 0) {
			page.setResult(getJdbcTemplate().query(page.getPagesql(sql),
					 new BeanPropertyRowMapper<RowVO>(RowVO.class)));
		} else {
			page.setResult(new ArrayList< RowVO>());
		}

		return page;
	}

	private String getcondition( RowQuery qvo) throws Exception{
		StringBuilder sql = new StringBuilder();
		sql.append(WHERE).append("1=1");		
      sqlappen(sql, "row.id", qvo.getId());sqlappen(sql, "row.rowname", qvo.getRowname());sqlappen(sql, "row.rowkey", qvo.getRowkey());sqlappen(sql, "row.rownum", qvo.getRownum());sqlappen(sql, "row.gmt_create", qvo.getGmtCreate());sqlappen(sql, "row.gmt_modified", qvo.getGmtModified());sqlappen(sql, "row.reportid", qvo.getReportid());		return sql.toString();
	}
	@Override
	public List<RowVO> list(RowQuery qvo) throws Exception{
		StringBuilder sql = new StringBuilder();
		sql.append(SELECT).append(QUERY_TABLE_COLUMN).append(FROM).append(QUERY_TABLE_NAME);
		sql.append(getcondition(qvo));
		return getJdbcTemplate().query(sql.toString(), new BeanPropertyRowMapper<RowVO>(RowVO.class));
	}
	
	@Override
	public void remove(BaseMap<String, Object> wheremap) {
		 baseremove(wheremap, "report_row");		
	}
	@Override
	public RowVO get(String id){	
		StringBuilder sql = new StringBuilder();
		sql.append(SELECT).append(QUERY_TABLE_COLUMN).append(FROM).append(QUERY_TABLE_NAME);
		sql.append(WHERE).append("1=1");
		sql.append(AND).append("id='"+id+"'");
		List<RowVO> list=getJdbcTemplate().query(sql.toString(), new BeanPropertyRowMapper<RowVO>(RowVO.class));		
		return QvoConditionUtil.checkList(list)?list.get(0):null;
	}
}
